package com.hxkcloud.nurse.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.MyReportListBean;
import com.hxkcloud.nurse.beans.ReportBean;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ProjectName: YJHCuzhong
 * PackageName: com.yjhcloud.stroke.activitys
 * Description:
 * Create by Ray.
 * Date: 2016/10/9 20:51
 */
public class MyReportExpandListActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private SwipeRefreshLayout swipeRefreshLayout;
    private ExpandableListView expandableListView;
    private MyExpandableListViewAdapter adapter;

    private List<String> group_list;
    private List<List<ReportBean>> item_list;

    private ObjectResultBean<MyReportListBean> resultBean;
    private String patientId = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_myreportexpandlist);
        if (getIntent().hasExtra("patientId")) {
            patientId = getIntent().getStringExtra("patientId");
        }
        initView();
        initListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_MYREPORTLIST_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<MyReportListBean>) msg.obj;

                    group_list.clear();
                    item_list.clear();
                    group_list.add(getResources().getString(R.string.myreportlist_text_common));
                    group_list.add(getResources().getString(R.string.myreportlist_text_all));
                    item_list.add(resultBean.getData().getCommonlyList());
                    Log.i("--------->",item_list.size()+"");
                    item_list.add(resultBean.getData().getAllList());
                    expandableListView.setAdapter(adapter);
                    setListViewHeight(expandableListView);
                    expandListView();
                }

                break;
            case MessageCode.NETWORKCODE_MYREPORTLIST_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(resultBean.getMessage());
                    return;
                }
                break;
        }
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(getResources().getString(R.string.myreportlist_text_title));

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.myreport_swiperefresh);
        expandableListView = (ExpandableListView) findViewById(R.id.myreport_expandlistview);
        expandableListView.setDividerHeight(0);

        //随便一堆测试数据
        group_list = new ArrayList<String>();
        item_list = new ArrayList<List<ReportBean>>();
        adapter = new MyExpandableListViewAdapter(MyReportExpandListActivity.this);
//        expandableListView.setAdapter(adapter);

        swipeRefreshLayout.setEnabled(true);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setProgressViewOffset(true, -20, 100);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        loading();
        patientId = getIntent().getStringExtra("patientId");
        YiJiaHuNetWorkUtils.getMyReportList(patientId,handler);
    }

    /**
     * 展开并group不可点击
     */
    private void expandListView() {
        for (int i = 0; i < group_list.size(); i++) {
            expandableListView.expandGroup(i);
        }
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                return true;
            }
        });
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Intent intent = new Intent(mContext, MyReportDetail1Activity.class);
                intent.putExtra("reportId", item_list.get(groupPosition).get(
                        childPosition).getReportId());
                intent.putExtra("reportName", item_list.get(groupPosition).get(
                        childPosition).getReportName());
                intent.putExtra("patientId",patientId);
                startActivity(intent);
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        initData();
        swipeRefreshLayout.setRefreshing(false);
    }


    //用过ListView的人一定很熟悉，只不过这里是BaseExpandableListAdapter
    class MyExpandableListViewAdapter extends BaseExpandableListAdapter {

        private Context context;

        public MyExpandableListViewAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getGroupCount() {
            return group_list.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return item_list.get(groupPosition).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return group_list.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return item_list.get(groupPosition).get(childPosition);
        }


        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            GroupHolder groupHolder = null;
            if (convertView == null) {
                convertView = (View) getLayoutInflater().from(context).inflate(
                        R.layout.myreport_expendlist_group, null);
                groupHolder = new GroupHolder();
                groupHolder.groupLayout = (LinearLayout) convertView.findViewById(R.id.myreport_expandlist_layout_groupitem);
                groupHolder.txt = (TextView) convertView.findViewById(R.id.myreport_group_name);

                convertView.setTag(groupHolder);
            } else {
                groupHolder = (GroupHolder) convertView.getTag();
            }
            groupHolder.txt.setText(group_list.get(groupPosition));


            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            ItemHolder itemHolder = null;
            if (convertView == null) {
                convertView = (View) getLayoutInflater().from(context).inflate(
                        R.layout.myreport_expendlist_item, null);
                itemHolder = new ItemHolder();
                itemHolder.txt = (TextView) convertView.findViewById(R.id.myreport_itemlist_name);
                itemHolder.bottomView = (View) convertView.findViewById(R.id.myreport_itemlist_bottomView);
                convertView.setTag(itemHolder);
            } else {
                itemHolder = (ItemHolder) convertView.getTag();
            }
            itemHolder.txt.setText(item_list.get(groupPosition).get(
                    childPosition).getReportName());

            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }

    class GroupHolder {
        public LinearLayout groupLayout;
        public TextView txt;
    }

    class ItemHolder {
        public TextView txt;
        public View bottomView;
    }


    private void setListViewHeight(ExpandableListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        int totalHeight = 0;
        int count = listAdapter.getCount();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

}
